<template>
    <div class="#app">
        <el-card>
       <!-- 2.1 顶部操作 -->

      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 使用clearable属性即可得到一个可清空的输入框   clear	在点击由 clearable 属性生成的清空按钮时触发-->
          <el-input placeholder="请输入楼层" v-model="queryInfo.floorId" class="input-with-select"  clearable @clear="getList">
            <template #append>
              <el-button icon="el-icon-search" @click="getList"></el-button>
            </template>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="showAddDialog">添加</el-button>
          <el-button type="primary" @click="deleteWithArr(multipleSelection)">批量删除</el-button>
        </el-col>
      </el-row>

    <el-table :data="list" border stripe
        @selection-change="handleSelectionChange"
      >
        <!-- 索引列 -->
        <el-table-column type="selection" width="60" ></el-table-column>
        <el-table-column label="楼层号" prop="floorId" width="60"></el-table-column>
        <el-table-column label="楼层名" prop="floorName"></el-table-column>
        <el-table-column label="备注" prop="remarks"></el-table-column>
        <!-- 自定义列 -->
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit" @click="showEditDialog(scope.row.id)">编辑
            </el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="removeById(scope.row.id)">删除
            </el-button>
            <!-- {{scope.row}} -->
          </template>
        </el-table-column>
      </el-table>

      <!-- 2.3 分页 -->

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum" :page-size="queryInfo.pageSize" :page-sizes="[5, 10, 20, 40]"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
         <!-- 3.弹框区域 -->

    <!-- 添加框 -->
    <!-- modify -->
    <el-dialog title="添加" :visible.sync="addDialogVisible" width="30%" @close="addDialogClosed">
      <el-form :model="addForm" ref="addFormRef" :rules="addFormRules" label-width="60px">
        <el-form-item label="楼层号" prop="floorId">
          <el-input v-model="addForm.floorId"></el-input>
        </el-form-item>
        <el-form-item label="楼层名" prop="floorName">
          <el-input v-model="addForm.floorName"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input type="textarea" v-model="addForm.remarks"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addFloor">确 定</el-button>
      </span>
    </el-dialog>
<!-- 编辑框 -->
<!-- modify -->
    <el-dialog title="编辑楼层信息" :visible.sync="editDialogVisible" width="30%" @close="editDialogClosed">
      <el-form :model="editForm" ref="editFormRef" :rules="editFormRules" label-width="50px">
        <el-form-item label="楼层号" prop="floorId">
          <el-input v-model="editForm.floorId"></el-input>
        </el-form-item>
        <el-form-item label="楼层名" prop="floorName">
          <el-input v-model="editForm.floorName"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input type="textarea" v-model="editForm.remarks"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </span>
    </el-dialog>
    </div>
</template>
<script>
  import { fecthList,add,update,deleteOne,deleteByIds, getById } from '../../api/floorInfo'; 
export default {
    data(){
        return {
            queryInfo: {
              floorId: "",
              pageNum: 1,
              pageSize: 10,
            },
            list: [],
            total: 0,

            addDialogVisible: false,
            adddialog: [],
            // modify
            addForm: {
              floorId: null,
              floorName: '',
              remarks: ''
            },
            // modify
            addFormRules: {
                floorName: [
                    {required: true,message: "楼层名称", trigger: "blur"},
                    { min: 1, max: 5, message: "长度在 1 到 5 个字符", trigger: "blur"}
                ],
                floorId: [
                  { required: true, message: '请输入摘要', trigger: 'blur' }
                ],
                remarks: [
                  { required: true, message: '请输入摘要', trigger: 'blur' }
                ],
            },

            editDialogVisible: false,
            // modify
            editForm: {},
            // modify
            editFormRules: {

            },

            ids: [],
            multipleSelection: [],
            
        }
    },
    created() {
        this.getList();
    },
    //方法
  methods: {
    //查询  async  await
    getList() {
      //{k1:v1,k2:v2}  ---> ?k1=v1&k2=v2    query=&pagenum=1&pagesize=10
      fecthList(this.queryInfo).then( response => {
        this.list = response.data.records
        this.total = response.data.total
      })
    },

    //分页
    handleSizeChange(val) {
      //console.log(`每页 ${val} 条`);
      this.queryInfo.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      //console.log(`当前页: ${val}`);
      this.queryInfo.pageNum = val; // 获取新页码
      this.getList();
    },

    //添加
    //弹框
    showAddDialog() {
      this.addDialogVisible = true;
    },

     addFloor() {
      this.$refs.addFormRef.validate( async (valid) => {
        //console.log(valid);

        if (!valid) return;

        //console.log(this.addForm);

        add(this.addForm).then(response => {
          this.addDialogVisible = false;
          if(response.status === 200){
            this.$notify({
            title: '成功',
            message: '添加成功',
            type: 'success',
            duration: 1000
            })
          }else{
            this.$notify({
            title: '失败',
            message: '添加失败',
            type: 'error',
            duration: 1000,
            })
          }
          this.getList();
        }).catch(() => {
          this.$notify({
            title: '失败',
            message: '请确保楼层号不重复！',
            type: 'error',
            duration: 1000,
            })
        })
      });
    },

    // @close="addDialogClosed"
    addDialogClosed() {
      //console.log(this.$refs); //可以获取所有带ref属性的元素
      this.$refs.addFormRef.resetFields(); //清空表单
    },

    //更新
    showEditDialog(id) {
    
      console.log("获取的id：" + id);
      getById(id).then(response => {
        if (response.status != 200) {
          return this.$message.error('查询信息失败！')
        }else{
        this.editForm = response.data // 对象复制
        console.log("获取的response.data：" + this.$qs.stringify(response.data));
        this.editDialogVisible = true
        }
        
      }).catch(()=>{
        return this.$message.error("获取失败")
      })  
    },
    update() {
      this.$refs.editFormRef.validate(async valid => {
          if (!valid) return
          update(this.editForm).then(response=>{
            // const index = this.list.findIndex(v => v.id === this.editForm.id)
            // this.list.splice(index, 1, this.editForm)
            if(response.status === 200){
              this.$notify({
                title: '修改成功',
                type: 'success',
                duration: 1200
              })
              this.getList()
            }else{
              return this.$message.error("修改失败")
            }
          })
          // 关闭对话框
          this.editDialogVisible = false
          })
          //
          // const {
          //   data: res
          // } = await this.$http.post(
          //   '/dict/update', this.editForm
          // )
          // if (res.code !== 200) {
          //   return this.$message.error('更新信息失败！')
          // }

          // // 提示修改成功
          // this.$message.success('更新信息成功！')
    },
    
    editDialogClosed() {
        this.$refs.editFormRef.resetFields()
    },
    removeById(id) {
      this.$confirm('此操作将永久删除数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // this.ids.push(id)
          // 删除逻辑
          deleteOne(id).then(res => {
            if(res.status === 200){
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 1200
              })
            }else{
              return this.$message.error("删除失败")
            }
            
            this.getList()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection)
    },
    deleteWithArr(val){
      val.forEach(element => {
        this.ids.push(element.id)
      });
      console.log("ids："+this.ids)

      deleteByIds(this.ids).then(res=>{
        if(res.status === 200){
          this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 1200
            })
        }else{
          return this.$message.error("删除失败！")
        }
        this.getList()
      })   
    }
  },
}
</script>
<style scoped>
.el-card {
  margin-top: 15px;
}

.el-table {
  margin-top: 15px;
  margin-bottom: 15px;
}
</style>
